﻿using Common;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

namespace CarPCBATest.Procedure
{
    internal class PcbConfigHelper : NotifyBase
    {
        static string fileName = "TestConfig.xml";
        public static AllPcbConfig AllPcbConfig { get; set; }

        public static AllPcbConfig CreateSample()
        {
            var listPowerConfig = new ObservableCollection<PowerConfig>()
            {
                new PowerConfig ()
                {
                    ChannelIndex = 0,
                        IsActive = true,
                        Current = 3,
                        Voltage= 13.5,
                },
                new PowerConfig ()
                {
                    ChannelIndex = 1,
                        IsActive = true,
                        Current = 3,
                        Voltage= 13.5,
                },
                new PowerConfig ()
                {
                    ChannelIndex = 2,
                        IsActive = true,
                        Current = 3,
                        Voltage= 5,
                }
            };

            var listDOConfig = new ObservableCollection<DOConfig>();
            foreach (DOFunction item in Enum.GetValues(typeof(DOFunction)))
            {
                var doConfig = new DOConfig(item, false);
                listDOConfig.Add(doConfig);
            }

            var singBoardConfig = new SingBoardConfig()
            {
                LedDesc = "Sample",
                Pattern = "",
                LowerLimitCurrent = 408,
                UpperLimitCurrent = 1907,
                ProcName = "SampleTest",
                WaitTime = 1000,
                ProcID = 10000,
                TestMethodName = "Method",
                SNLength = 1,
                ListPowerConfig = listPowerConfig,
                ListDOConfig = listDOConfig,
            };

            PcbConfig pcbConfig = new PcbConfig()
            {
                No = 0,
                Name = "A控制板L",
                VisionProjPath = "VisionProjPath",
                ProbeMaxUsedTimes = 10000,
                ProbeUsedTimes = 0,
                ListSingBoardConfig = new ObservableCollection<SingBoardConfig>
                {
                    singBoardConfig
                }
                ,
            };

            AllPcbConfig config = new AllPcbConfig()
            {
                DM3025EPort = "COM1",
                IV3003TPort = "COM3",
                Title = "Title",
                UsingNo = 0,
                ListConfig = new ObservableCollection<PcbConfig> { pcbConfig }
            };

            XmlSerializer serializer = new XmlSerializer(typeof(AllPcbConfig));
            TextWriter writer = new StreamWriter(fileName);
            // 初始化默认值

            serializer.Serialize(writer, config);
            writer.Close();
            return config;
        }

        public static PcbConfig GetTestConfig()
        {
            var config = Load();
            return config.ListConfig.Where(t => t.No == config.UsingNo).ToList()[0];
        }

        public static AllPcbConfig Load()
        {
            if (!File.Exists(fileName))
            {
                return CreateSample();
            }
            XmlSerializer serializer = new XmlSerializer(typeof(AllPcbConfig));

            TextReader reader = new StreamReader(fileName);
            // 初始化默认值

            var obj = serializer.Deserialize(reader);
            reader.Close();
            AllPcbConfig = obj as AllPcbConfig;
            return obj as AllPcbConfig;
        }

        public static void Save(AllPcbConfig config)
        {
            AllPcbConfig = config;
            XmlSerializer serializer = new XmlSerializer(typeof(AllPcbConfig));
            TextWriter writer = new StreamWriter(fileName);
            // 初始化默认值

            serializer.Serialize(writer, config);
            writer.Close();
        }

        public static void Save()
        {
            var config = AllPcbConfig;
            XmlSerializer serializer = new XmlSerializer(typeof(AllPcbConfig));
            TextWriter writer = new StreamWriter(fileName);
            // 初始化默认值

            serializer.Serialize(writer, config);
            writer.Close();
        }
    }
}